﻿using DotNetDAL.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;

namespace DotNetDAL.Connector
{
   public interface IConnector
    {
        /// <summary>
        /// 释放资源
        /// </summary>
        void Dispose();
        /// <summary>
        /// 工具函数
        /// </summary>
        ConnectorUtils Utils { get; }
        /// <summary>
        /// 获取数据连接
        /// </summary>
        /// <returns></returns>
        DbConnection GetConnection();
        /// <summary>
        /// 关闭数据库连接
        /// </summary>
        void CloseConnection();
        /// <summary>
        /// 打开数据库连接
        /// </summary>
        void OpenConnection();
        /// <summary>
        /// 创建数据库命令
        /// </summary>
        /// <param name="SQL">sql语句</param>
        /// <param name="paramObj">sql参数</param>
        /// <param name="cmdType"></param>
        /// <param name="CommandTimeout">超时时间。以秒为单位</param>
        /// <returns></returns>
        DbCommand CreateCommand(string SQL, object paramObj, CommandType cmdType, int CommandTimeout = 60);
        /// <summary>
        /// 执行SQL语句或存储过程
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paramObj">参数</param>
        /// <param name="cmdType">命令类型</param>
        /// <param name="IsTransaction">是否使用事务</param>
        /// <returns>返回受影响的行数</returns>
        int ExecuteNonQuery(string sql, object paramObj = null, CommandType cmdType= CommandType.Text, bool IsTransaction=false);
        /// <summary>
        /// 使用事务，批量执行SQL语句，返回受影响的行数
        /// </summary>
        /// <param name="pars">执行参数对象</param>
        /// <returns></returns>
        int ExecuteNonQuery(List<SqlParams> pars);
        /// <summary>
        /// 执行SQL语句或存储过程
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paramObj">参数</param>
        /// <param name="cmdType">命令类型</param>
        /// <returns></returns>
        DbDataReader ExecuteReader(string sql, object paramObj = null, CommandType cmdType = CommandType.Text);
        /// <summary>
        /// 执行SQL语句或存储过程（返回查询所返回的结果集中的第一行第一列）
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paramObj">参数</param>
        /// <param name="cmdType">命令类型</param>
        /// <returns>返回查询所返回的结果集中的第一行第一列</returns>
        object ExecuteScalar(string sql, object paramObj = null, CommandType cmdType = CommandType.Text);
        /// <summary>
        /// 执行SQL语句或存储过程（返回查询所返回的结果集中的第一行第一列）
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paramObj">参数</param>
        /// <param name="cmdType">命令类型</param>
        /// <returns>返回查询所返回的结果集中的第一行第一列</returns>
        string ExecuteScalarString(string sql, object paramObj = null, CommandType cmdType = CommandType.Text);
        /// <summary>
        /// 执行SQL语句或存储过程
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paramObj">参数</param>
        /// <param name="cmdType">执行类型</param>
        /// <returns></returns>
        DataTable ExecuteDataTable(string sql, object paramObj = null, CommandType cmdType = CommandType.Text);
        /// <summary>
        /// 执行SQL语句或存储过程
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="paramObj">参数</param>
        /// <param name="cmdType">执行类型</param>
        /// <returns></returns>
        DataSet ExecuteDataSet(string sql, object paramObj = null, CommandType cmdType = CommandType.Text);
        /// <summary>
        /// 执行分页查询（在数据库分页）
        /// </summary>
        /// <param name="PageSize">每页大小</param>
        /// <param name="PageNumber">页码</param>
        /// <param name="OrderBy">排序参数。比如：“bar_sn desc,CreateTime asc”、“bar_sn asc”、“bar_sn”。</param>
        /// <param name="FromBefore">SQL语句 From 前面部分</param>
        /// <param name="FromAndBehind">SQL语句 From 本身及其后面部分</param>
        /// <param name="paramObj">参数</param>
        /// <param name="IsUseTransaction">是否使用事务</param>
        /// <returns></returns>
        PagingData ExecutePage(int PageSize, int PageNumber, string OrderBy, string FromBefore, string FromAndBehind, object paramObj = null, bool IsUseTransaction = false);
        /// <summary>
        /// 执行分页查询（在数据库分页）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="PageSize">每页大小</param>
        /// <param name="PageNumber">页码</param>
        /// <param name="OrderBy">排序参数。比如：“bar_sn desc,CreateTime asc”、“bar_sn asc”、“bar_sn”。</param>
        /// <param name="FromBefore">SQL语句 From 前面部分</param>
        /// <param name="FromAndBehind">SQL语句 From 本身及其后面部分</param>
        /// <param name="paramObj">参数</param>
        /// <param name="IsUseTransaction">是否使用事务</param>
        /// <returns></returns>
        PagingData<T> ExecutePage<T>(int PageSize, int PageNumber, string OrderBy, string FromBefore, string FromAndBehind, object paramObj = null, bool IsUseTransaction = false) where T : class;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="table_name"></param>
        /// <param name="paramObj"></param>
        bool Add(string table_name, object paramObj);
        /// <summary>
        /// 实体操作-更新数据
        /// </summary>
        /// <param name="table_name">表名</param>
        /// <param name="updateFieldParam">待更新的实体</param>
        /// <param name="whereSQL">where条件SQL</param>
        /// <param name="whereParamObj">where条件SQL参数</param>
        /// <returns></returns>
        bool Update(string table_name, object updateFieldParam, string whereSQL, object whereParamObj = null);
        /// <summary>
        /// 实体操作-更新数据
        /// </summary>
        /// <param name="table_name">表名</param>
        /// <param name="updateFieldParam">待更新的实体</param>
        /// <param name="whereParam">where条件参数</param>
        /// <returns></returns>
        bool Update(string table_name, object updateFieldParam, WhereParameter whereParam);
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="procName">存储过程名</param>
        /// <param name="procReturn">返回值</param>
        /// <param name="procOutputs">输出参数</param>
        /// <param name="paramObj">参数</param>
        /// <returns></returns>
        int ExecuteProc(string procName, out int procReturn, out Dictionary<string, object> procOutputs, object paramObj = null);
        /// <summary>
        /// 执行存储过程，返回数据表
        /// </summary>
        /// <param name="procName">存储过程名</param>
        /// <param name="procReturn">返回值</param>
        /// <param name="procOutputs">输出参数</param>
        /// <param name="paramObj">参数</param>
        /// <returns></returns>
        DataTable ExecuteProcDataTable(string procName, out int procReturn, out Dictionary<string, object> procOutputs, object paramObj = null);
        /// <summary>
        /// 执行存储过程，返回数据集
        /// </summary>
        /// <param name="procName">存储过程名</param>
        /// <param name="procReturn">返回值</param>
        /// <param name="procOutputs">输出参数</param>
        /// <param name="paramObj">参数</param>
        /// <returns></returns>
        DataSet ExecuteProcDataSet(string procName, out int procReturn, out Dictionary<string, object> procOutputs, object paramObj = null);
        /// <summary>
        /// 执行事务。  ExecuteTransaction( tran => {});
        /// </summary>
        /// <param name="action"></param>
        void ExecuteTransaction(Action<ITransactor> action);

    }
}
